package com.dj.mylibrary.router;


import com.dj.mylibrary.param.UriParam;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;

import java.io.Serializable;
import java.util.HashMap;

public final class Router {

    private static Router mRouter;
    private Router(){
    }


    public static Router getInstance() {
        if(null==mRouter){
            mRouter=new Router();
        }
        return mRouter;
    }


    public void startJump(AbilitySlice slice, UriParam param, HashMap<String,Object> data, Class<?extends AbilitySlice> cls) {
//        boolean jump = false;
        Intent intent = new Intent();
        if (param != null) {
            Operation ope = new Intent.OperationBuilder()
                    .withBundleName(param.getBundleName())
                    .withAbilityName(param.getAbilityName())
                    .build();
            intent.setOperation(ope);
            if(injectData(intent,data)){
                slice.startAbility(intent);
            }
        }else{
            if(injectData(intent,data)){
                try {
                    Object obj=cls.newInstance();
                    slice.present((AbilitySlice) obj,intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public boolean injectData(Intent intent,HashMap<String,Object> data){
        if (data != null) {
            for (String key : data.keySet()) {
                Object value = data.get(key);

                if (value instanceof Integer) {
                    intent.setParam(key, Integer.parseInt(value.toString()));
                } else if (value instanceof Long) {
                    intent.setParam(key, Long.parseLong(value.toString()));
                } else if (value instanceof Double) {
                    intent.setParam(key, Double.parseDouble(value.toString()));
                } else if (value instanceof Short) {
                    intent.setParam(key, Short.parseShort(value.toString()));
                } else if (value instanceof Float) {
                    intent.setParam(key, Float.parseFloat(value.toString()));
                } else if (value instanceof String) {
                    intent.setParam(key, (String) value);
                } else if (value instanceof Serializable) {
                    intent.setParam(key, (Serializable) value);
                } else if (value.getClass().isArray()) {
                    if (int[].class.isInstance(value)) {
                        intent.setParam(key, (int[]) value);
                    } else if (long[].class.isInstance(value)) {
                        intent.setParam(key, (long[]) value);
                    } else if (double[].class.isInstance(value)) {
                        intent.setParam(key, (double[]) value);
                    } else if (short[].class.isInstance(value)) {
                        intent.setParam(key, (short[]) value);
                    } else if (float[].class.isInstance(value)) {
                        intent.setParam(key, (float[]) value);
                    } else if (String[].class.isInstance(value)) {
                        intent.setParam(key, (String[]) value);
                    }
                }
            }
            return true;
        }
        return false;
    }


//    public HashMap<String,Object> inject(Intent intent, InjectParam param){
//        HashMap<String,Object> data=new HashMap<>();
//        if(param.getInject()!=null){
//            String[] inject=param.getInject();
//            for(String key:inject){
//                Object value=intent.getParam(key);
//                if(!isEmpty(value)){
//                    data.put(key,value);
//                }
//            }
//        }
//
//        return data;
//    }


    private Boolean isEmpty(Object obj){
        return (obj==null)||(obj.toString().trim()=="");
    }

}
